#!/bin/bash
clear

# INFO='\033[97;44m'
# WARNING='\033[97;43m'
# SUCCESS='\033[97;42m'
# GREEN='\033[92m'
# YELLOW='\033[93m'
TEXT='\033[30;107;2m'
ERROR='\033[97;41m'
RESET='\033[0m'

if [ "${EUID}" -ne 0 ]; then
  echo -e "${ERROR} ERROR ${RESET}${TEXT} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

checkClientName=$(grep -c -E "^### Client" "/etc/wireguard/wg0.conf")
if [[ ${checkClientName} == '0' ]]; then
  echo "You have no existing clients!"
  exit 1
fi

echo
echo "Select the existing client you want to remove"
grep -E "^### START" "/etc/wireguard/wg0.conf" | cut -d ' ' -f 3 | nl -s ') '
until [[ ${ClientNum} -ge 1 && ${ClientNum} -le ${checkClientName} ]]; do
  if [[ ${ClientNum} == '1' ]]; then
    read -rp "Select one client [1]: " ClientNum
  else
    read -rp "Select one client [1-${checkClientName}]: " ClientNum
  fi
done

clientName=$(grep -E "^### START" "/etc/wireguard/wg0.conf" | cut -d ' ' -f 3 | sed -n "${ClientNum}"p)
# sed -i "/^### START ${clientName}\$/,/^$/d" "/etc/wireguard/wg0.conf"
sed -i "/\### START $clientName/,/\### END $clientName/d" /etc/wireguard/wg0.conf
sed -i "/$clientName/d" /etc/wireguard/clients/.accounts
rm -rf /etc/wireguard/clients/$clientName

systemctl restart wg-quick@wg0

clear
echo
echo -e "${TEXT} ========================================================== ${RESET}"
echo -e "${TEXT} WIREGUARD - PADAM AKAUN PENGGUNA                           ${RESET}"
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo
echo "Pengguna $clientName sudah berjaya di padam."
echo
echo -e "${TEXT} ---------------------------------------------------------- ${RESET}"
echo -e "${TEXT} Dicipta oleh Doctype, Dikuasakan oleh Cybertize.           ${RESET}"
echo -e "${TEXT} ========================================================== ${RESET}"
echo
